package com.lanp.native_test.ExecutorsRun.CustomTheadExecutor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;

public class DelayQueueTest {
    /*
    无边界、延时阻塞队列、根据过期时间执行
    最大任务容量是 最大线程数 + 队列最大长度
    不超过 阻塞队列数量，执行核心线程数量
    超过 阻塞队列数量，执行最大线程数量
     */
    static BlockingQueue blockingQueue = new DelayQueue();


    private static final ThreadPoolExecutor TEST_EXECUTORS =
            new ThreadPoolExecutor(1, 4, 10, TimeUnit.MINUTES
                    , blockingQueue, Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws InterruptedException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < 20; i++) {
            long delayTime = System.currentTimeMillis() + i * 10 * 1000;
            Date dd = new Date();
            dd.setTime(delayTime);
            System.out.println("准备添加到队列" + i + "预计执行时间：" + sdf.format(delayTime));
            TestDelayRunnable runnable = new TestDelayRunnable(delayTime);
            TEST_EXECUTORS.execute(runnable);
        }

    }
}
